package com.web.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.web.entity.User;
import com.web.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    public List<User> findAll(){
        return this.userMapper.selectList(null);
    }
    public User findById(Integer id){
        return this.userMapper.selectById(id);
    }

    // 用户登录
    public User login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username)
                .eq("password", password);
        return userMapper.selectOne(queryWrapper);
    }
    // 用户注册
    public boolean register(User user) {
        // 先检查用户名是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        User existingUser = userMapper.selectOne(queryWrapper);

        if (existingUser != null) {
            // 用户名已存在，注册失败
            return false;
        }

        // 用户名不存在，插入新用户
        int rows = userMapper.insert(user);
        return rows > 0;
    }
}
